//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DotNetNuke.UI.Containers
{
	partial class Visibility : UI.Skins.SkinObjectBase
	{
		private string _borderWidth;
		private string _minIcon;
		private string _maxIcon;
		private int _animationFrames = 5;
		private Entities.Modules.PortalModuleBase m_objPortalModule;
		private Panel m_pnlModuleContent;
		private string MinIconLoc {
			get {
				if (!String.IsNullOrEmpty(minIcon)) {
					return ModulePath + minIcon;
				} else {
					return Common.Globals.ApplicationPath + "/images/min.gif";
				}
			}
		}
		private string MaxIconLoc {
			get {
				if (!String.IsNullOrEmpty(MaxIcon)) {
					return ModulePath + MaxIcon;
				} else {
					return Common.Globals.ApplicationPath + "/images/max.gif";
				}
			}
		}
		private Panel ModuleContent {
			get {
				if (m_pnlModuleContent == null) {
					Control objCtl = this.Parent.FindControl("ModuleContent");
					if (objCtl != null)
						m_pnlModuleContent = (Panel)objCtl;
				}
				return m_pnlModuleContent;
			}
		}
		private string ModulePath {
			get { return ModuleControl.ModuleContext.Configuration.ContainerPath.Substring(0, ModuleControl.ModuleContext.Configuration.ContainerPath.LastIndexOf("/") + 1); }
		}
		public int AnimationFrames {
			get { return _animationFrames; }
			set { _animationFrames = value; }
		}
		public string BorderWidth {
			get { return _borderWidth; }
			set { _borderWidth = value; }
		}
		public bool ContentVisible {
			get {
				switch (ModuleControl.ModuleContext.Configuration.Visibility) {
					case Entities.Modules.VisibilityState.Maximized:
					case Entities.Modules.VisibilityState.Minimized:
						return DotNetNuke.UI.Utilities.DNNClientAPI.MinMaxContentVisibile(cmdVisibility, ModuleControl.ModuleContext.ModuleId, ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.Minimized, Utilities.DNNClientAPI.MinMaxPersistanceType.Cookie);
					default:
						return true;
				}
			}
			set { DotNetNuke.UI.Utilities.DNNClientAPI.MinMaxContentVisibile(cmdVisibility, ModuleControl.ModuleContext.ModuleId, ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.Minimized, Utilities.DNNClientAPI.MinMaxPersistanceType.Cookie, value); }
		}
		public string minIcon {
			get { return _minIcon; }
			set { _minIcon = value; }
		}
		public string MaxIcon {
			get { return _maxIcon; }
			set { _maxIcon = value; }
		}
		public string ResourceFile {
			get { return Services.Localization.Localization.GetResourceFile(this, "Visibility.ascx"); }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdVisibility.Click += new EventHandler(cmdVisibility_Click);

			try {
				if (!Page.IsPostBack) {
					if (!String.IsNullOrEmpty(BorderWidth)) {
						cmdVisibility.BorderWidth = System.Web.UI.WebControls.Unit.Parse(BorderWidth);
					}
					if (ModuleControl.ModuleContext.Configuration != null) {
						if (ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.None) {
							cmdVisibility.Enabled = false;
							cmdVisibility.Visible = false;
						}
						if (ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.Minimized) {
							DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(this.Page, "__dnn_" + ModuleControl.ModuleContext.ModuleId.ToString() + ":defminimized", "true", true);
						}
						if (!Common.Globals.IsAdminControl()) {
							if (cmdVisibility.Enabled) {
								if (ModuleContent != null) {
								} else {
									this.Visible = false;
								}
							}
						} else {
							this.Visible = false;
						}
					} else {
						this.Visible = false;
					}
				} else {
					if (ModuleControl.ModuleContext.Configuration != null) {
						if (ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.None) {
							cmdVisibility.Enabled = false;
							cmdVisibility.Visible = false;
						}
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected override void  OnPreRender(EventArgs e)
		{
 			base.OnPreRender(e);
			if (ModuleContent != null && ModuleControl != null && !Common.Globals.IsAdminControl()) {
				switch (ModuleControl.ModuleContext.Configuration.Visibility) {
					case Entities.Modules.VisibilityState.Maximized:
					case Entities.Modules.VisibilityState.Minimized:
						DotNetNuke.UI.Utilities.DNNClientAPI.EnableMinMax(cmdVisibility, ModuleContent, ModuleControl.ModuleContext.ModuleId, ModuleControl.ModuleContext.Configuration.Visibility == Entities.Modules.VisibilityState.Minimized, MinIconLoc, MaxIconLoc, Utilities.DNNClientAPI.MinMaxPersistanceType.Cookie, this.AnimationFrames);
						break;
				}
			}
		}
		private void cmdVisibility_Click(System.Object sender, System.EventArgs e)
		{
			try {
				if (ModuleContent != null) {
					if (ModuleContent.Visible == true) {
						this.ContentVisible = false;
					} else {
						this.ContentVisible = true;
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
